Polymorphic Relationship

  • STEP

    Models

    post model

    
           
    namespace App\Models;
    use Illuminate\Database\Eloquent\Model;
     
    class Post extends Model
    {
        /**
         * Get all of the post's comments.
         */
        public function comments()
        {
            return $this->morphMany(Comment::class, 'commentable');
        }
    }
    
    
    Note morphMany()

    Video Model:

    
            namespace App\Models;
     
     use Illuminate\Database\Eloquent\Model;
      
     class Video extends Model
     {
         /**
          * Get all of the post's comments.
          */
         public function comments()
         {
             return $this->morphMany(Comment::class, 'commentable');
         }
     }
     
    
    Note morphMany()

    Comment Model:

    
    namespace App\Models;
     
     use Illuminate\Database\Eloquent\Model;
      
     class Comment extends Model
     {
         /**
          * Get all of the owning commentable models.
          */
         public function commentable()
         {
             return $this->morphTo();
         }
     }
    
    Note morphTo()

    Retrieve Records:

    
    
    $post = Post::find(1);	
     
    dd($post->comments);
    
    
    $video = Video::find(1);	
     
    dd($video->comments);
    
    

    Create Records:

    
    
    $post = Post::find(1);	
     
    $comment = new Comment;
    $comment->body = "Hi ItSolutionStuff.com";
     
    $post->comments()->save($comment);
    
    
    
    
    
    $video = Video::find(1);	
     
    $comment = new Comment;
    $comment->body = "Hi ItSolutionStuff.com";
     
    $video->comments()->save($comment);
    
    
                        </div>
                        </li>
    
    
    
               
              </ul>
            </div>
    
    
    
    </div>       
    
    
    
          </div> 
        </section>     
    
    
    
    
    
        
    
      </main><!-- End #main -->
    
      
    
      <div id="preloader"></div>
      <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>
    
      <!-- Vendor JS Files -->
      <script src="https://blog.manvia.in/public/assets/vendor/aos/aos.js"></script>
      <script src="https://blog.manvia.in/public/assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
      <script src="https://blog.manvia.in/public/assets/vendor/glightbox/js/glightbox.min.js"></script>
      <script src="https://blog.manvia.in/public/assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
      <script src="https://blog.manvia.in/public/assets/vendor/php-email-form/validate.js"></script>
      <script src="https://blog.manvia.in/public/assets/vendor/swiper/swiper-bundle.min.js"></script>
      <script src="https://blog.manvia.in/public/assets/vendor/waypoints/noframework.waypoints.js"></script>
    
      <!-- Template Main JS File -->
      <script src="https://blog.manvia.in/public/assets/js/main.js"></script>
    
    </body>
    
    </html>